package com.lib.video.widget;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Point;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Display;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.ImageView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.lib.video.R;

import java.util.ArrayList;

public class VideoRangeProgressBar extends FrameLayout {

    private boolean isInit = false;

    // 记录左右滑动的记录位置
    private RangePoint leftPointR;
    private RangePoint rightPointR;

    private Paint mPaintR;
    private Paint mPaint;

    private final int PROGRESS_HEIGHT_WEIGHT = 10; // 垂直栏圈的高度
    private final int PROGRESS_WIDTH_WEIGHT = 30; // 水平栏圈的宽度
    private int mProgressWidth; // 进度栏的宽度
    private int mProgressHeight; // 进度栏的高度
    private int mWidthSize;
    private int mHeightSize;

    private final int MAXSECOUND = 30; // 最大切割秒数
    private final int MINSECOUND = 10; // 最小切换秒数

    private int min_width;

    public VideoRangeProgressBar(@NonNull Context context) {
        this(context, null);
    }

    public VideoRangeProgressBar(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public VideoRangeProgressBar(@NonNull Context context, @Nullable AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init(context);
        setClickable(true);

        setWillNotDraw(false);
    }

    private void init(Context context) {
        mPaint = new Paint();
        mPaint.setColor(Color.BLUE);

        mPaintR = new Paint();
        mPaintR.setColor(Color.BLUE);

        WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        Display display = windowManager.getDefaultDisplay();
        DisplayMetrics dm = new DisplayMetrics();
        display.getMetrics(dm);
        mProgressWidth = dm.widthPixels;

        min_width = mProgressWidth / MAXSECOUND * MINSECOUND;

        initRecyclerView(context);
    }

    private void initRecyclerView(Context context) {
        RecyclerView recyclerView = new RecyclerView(context);
        LinearLayoutManager manager = new LinearLayoutManager(context);
        manager.setOrientation(LinearLayoutManager.HORIZONTAL);
        recyclerView.setLayoutManager(manager);
    }

    @Override
    protected void onMeasure(int widthSpec, int heightSpec) {
        Log.d("ttt", "onMeasure");
        super.onMeasure(widthSpec, heightSpec);
        if (!isInit) {
            leftPointR = new RangePoint(0, 0);
            rightPointR = new RangePoint(mProgressWidth, 0);
            mProgressHeight = getMeasuredHeight();
            isInit = true;
        }
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        Log.d("ttt", "onLayout");
        super.onLayout(changed, left, top, right, bottom);
    }

    @Override
    public void onDraw(Canvas canvas) {
        Log.d("ttt", "onDraw");
        super.onDraw(canvas);
        drawProgress(canvas);
    }

    private void drawProgress(Canvas canvas) {
        canvas.drawRect(leftPointR.x, 0, rightPointR.x, PROGRESS_HEIGHT_WEIGHT, mPaint);
        canvas.drawRect(leftPointR.x, mProgressHeight - PROGRESS_HEIGHT_WEIGHT, rightPointR.x, mProgressHeight, mPaint);

        canvas.drawRect(leftPointR.x, 0, leftPointR.x + PROGRESS_WIDTH_WEIGHT, mProgressHeight, mPaintR);
        canvas.drawRect(rightPointR.x - PROGRESS_WIDTH_WEIGHT, 0, rightPointR.x, mProgressHeight, mPaintR);

        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inPreferredConfig = Bitmap.Config.RGB_565;
        Bitmap cRight = BitmapFactory.decodeResource(getResources(), R.mipmap.range_right, options);
        Matrix matrix = new Matrix();
        matrix.setRotate(180, cRight.getWidth() * 0.5f, cRight.getHeight() * 0.5f);
        Bitmap cLeft = Bitmap.createBitmap(cRight, 0, 0, cRight.getWidth(), cRight.getHeight(), matrix, true);
        canvas.drawBitmap(cLeft, leftPointR.x + PROGRESS_WIDTH_WEIGHT / 2 - cLeft.getWidth() / 2, mHeightSize / 2 - cLeft.getHeight() / 2, mPaint);
        canvas.drawBitmap(cRight, rightPointR.x - PROGRESS_WIDTH_WEIGHT / 2 - cRight.getWidth() / 2, mHeightSize / 2 - cLeft.getHeight() / 2, mPaint);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);

        mWidthSize = w;
        mHeightSize = h;
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent e) {
        if (!touchProgress(e)) {
            return super.dispatchTouchEvent(e);
        } else {
            return false;
        }
    }

    private boolean touchProgress(MotionEvent e) {
        Log.d("ttt", "touchProgress---" + e.getAction());
        if (e.getAction() == MotionEvent.ACTION_DOWN) {
            float x = e.getX();
            leftPointR._isTouchR = false;
            rightPointR._isTouchR = false;
            if (leftPointR.x < x && leftPointR.x + PROGRESS_WIDTH_WEIGHT > x) {
                leftPointR._isTouchR = true;
            }
            if (rightPointR.x > x && rightPointR.x - PROGRESS_WIDTH_WEIGHT < x) {
                rightPointR._isTouchR = true;
            }
        } else if (e.getAction() == MotionEvent.ACTION_MOVE) {
            if (leftPointR._isTouchR || rightPointR._isTouchR) {
                if (leftPointR._isTouchR) {
                    int rx = (int) ((e.getX() < 0) ? 0 : e.getX());
                    rx = (rx > mProgressWidth) ? mProgressWidth : rx;
                    rx = (rightPointR.x - rx) > min_width ? rx : (rightPointR.x - min_width);
                    Log.d("ttt", "rx---" + rx);
                    leftPointR.x = rx;
                    invalidate();
                }
                if (rightPointR._isTouchR) {
                    int rx = (int) ((e.getX() < 0) ? 0 : e.getX());
                    rx = (rx > mProgressWidth) ? mProgressWidth : rx;
                    rx = (rx - leftPointR.x) > min_width ? rx : (leftPointR.x + min_width);
                    rightPointR.x = rx;
                    invalidate();
                }
            }
        } else if (e.getAction() == MotionEvent.ACTION_UP) {
            if (leftPointR._isTouchR || rightPointR._isTouchR) {
                leftPointR._isTouchR = false;
                rightPointR._isTouchR = false;
            }
        }
        return false;
    }

    private class RangePoint extends Point {
        public boolean _isTouchR;

        public RangePoint(int x, int y) {
            super(x, y);
        }
    }

    class FrameAdapter extends BaseQuickAdapter<Long, BaseViewHolder> {

        public FrameAdapter() {
            super(new ArrayList<Long>());
        }

        @Override
        protected void convert(BaseViewHolder helper, Long item) {

        }

        @Override
        protected View getItemView(int layoutResId, ViewGroup parent) {
            ImageView imageView = new ImageView(parent.getContext());
            return imageView;
        }
    }
}
